<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\model\OrderModel;
use app\model\WareModel;
use think\facade\Request;
use think\facade\Validate;

class Ware
{
    public function index()
    {
        $wareList=WareModel::select();
        $data=[
            'status'=>0,
            'message'=>'',
            'data'=>[
                'wareList'=>$wareList
            ],
        ];
        // foreach ($wareList as &$item) {
        //     $wareList = WareModel::where('ware_id', "=", $item['ware_id'])->select();
        //     $item['categoryList'] = $categoryList->toArray();
        // }
        // unset($item);
        return json ($data);
    }
    public function list(){
        $categoryId=Request::param('category_id');
        $validate=Validate::rule([
            'category_id|所属分类id'=>'require|between"1,'.PHP_INT_MAX
        ]);
        if (!$validate->check(['categoryId'=>$categoryId])){
            $data=[
                'status'=>'1',
                'message'=>$validate->getError(),
                'data'=>[]
            ];
            return json($data);
        }
        $result=WareModel::where("category_id","=",$categoryId)
            ->order("add_time","desc")
            ->select();
        $wareList=[];
        foreach ($result as $item){
            $temp=$item->toArray();
            $wareList[]=[
                'ware_id'=>$temp['ware_id'],
                'category_id'=>$temp['category_id'],
                'ware_name'=>$temp['ware_name'],
                'ware_image'=>$temp['ware_image'],
                'ware_describe'=>$temp['ware_describe'],
                'ware_type'=>$temp['ware_type'],
                'ware_price'=>$temp['ware_price'],
                'ware_num'=>$temp['ware_num'],
                'add_time'=>$temp['add_time'],
                'update_time'=>$temp['update_time'],
            ];
        }
        $data=[
            'status'=>'0',
            'message'=>'',
            'data'=>[
                'wareList'=>$wareList
            ]
        ];
        return json($data);
    }
}
